/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.web.jsf.beans.user;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.richfaces.component.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.greensoft.neo.dao.PersonaFisicaDao;
import ar.com.greensoft.neo.model.PersonaFisica;
import ar.com.greensoft.neo.web.jsf.beans.common.DaoDataModel;

import com.google.common.collect.Maps;

/**
 * @author Jorge E. Villaverde
 * 
 */
@Component
@ManagedBean
@SessionScoped
public class EjemploBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7609996141113078791L;
	@Autowired
	private PersonaFisicaDao dao;
    private Map<String, SortOrder> sortOrders = Maps.newHashMapWithExpectedSize(1);
    private Map<String, String> filterValues = Maps.newHashMap();
    private String sortProperty;
    
	private static final class PersonaFisicaDataModel extends DaoDataModel<PersonaFisica, Long> {
		private PersonaFisicaDataModel(PersonaFisicaDao dao) {
			super(dao);
		}

		@Override
		protected Object getId(PersonaFisica t) {
			return t.getId();
		}
	}
	
 
    public EjemploBean() {
        sortOrders.put("apellido1", SortOrder.unsorted);
        sortOrders.put("apellido2", SortOrder.unsorted);
        sortOrders.put("primerNombre", SortOrder.unsorted);
        sortOrders.put("segundoNombre", SortOrder.unsorted);
    }

    
    public Map<String, SortOrder> getSortOrders() {
        return sortOrders;
    }
 
    public Map<String, String> getFilterValues() {
        return filterValues;
    }
 
    public String getSortProperty() {
        return sortProperty;
    }
 
    public void setSortProperty(String sortPropety) {
        this.sortProperty = sortPropety;
    }
 
    public void toggleSort() {
        for (Entry<String, SortOrder> entry : sortOrders.entrySet()) {
            SortOrder newOrder;
 
            if (entry.getKey().equals(sortProperty)) {
                if (entry.getValue() == SortOrder.ascending) {
                    newOrder = SortOrder.descending;
                } else {
                    newOrder = SortOrder.ascending;
                }
            } else {
                newOrder = SortOrder.unsorted;
            }
 
            entry.setValue(newOrder);
        }
    }
 
    public Object getDataModel() {
        return new PersonaFisicaDataModel(dao);
    }
}
